﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            pictureBox1.ImageLocation = openFileDialog1.FileName;
        }

        private void button2_Click(object sender, EventArgs e)
        {/*
            Rectangle r = pictureBox1.RectangleToScreen(pictureBox1.ClientRectangle);
            Bitmap b = new Bitmap(175, 175);
            Graphics g = Graphics.FromImage(b);
            Size sss = new Size();
            sss.Width = 250;
            sss.Height = 250;
            Point s = new Point(-23, 175);
            g.CopyFromScreen(s, new Point(0, 0), sss);
            b.Save("c:/111.jpg");
          */
            //CopyRest(0, 0,175, 175);
            int sCoutWidth, sCoutHeigth;
            sCoutWidth = pictureBox1.ClientRectangle.Width / 175;
            sCoutHeigth = pictureBox1.ClientRectangle.Height / 175;
            label1.Text = sCoutWidth.ToString() + "X" + sCoutHeigth.ToString();
            for (int i = 0; i < sCoutWidth; i++)
            {
                for (int j = 0; j < sCoutHeigth; j++)
                {
                    //MessageBox.Show(i.ToString() + "x" + j.ToString());
                    CopyRest(j * 175, i * 175, 175, 175, i, j);
                }
            }
        }
        private void CopyRest(int x, int y, int w, int h, int xF, int yF)
        {
            Rectangle r = pictureBox1.RectangleToScreen(pictureBox1.ClientRectangle);
            Bitmap b = new Bitmap(w, h);
            Graphics g = Graphics.FromImage(b);
            Size sss = new Size();
            sss.Width = w;
            sss.Height = h;
            
            Point s = new Point(w, h);
            g.CopyFromScreen(new Point(r.Location.X+x,r.Location.Y+y), new Point(0, 0), new Size(w,h));
            b.Save("c:/"+xF.ToString()+"x"+yF.ToString()+".jpg");

        }

        private void CopyRest1(int x, int y, int w, int h, int xF, int yF)
        {
            Bitmap b = new Bitmap(w, h);
            Graphics gr = Graphics.FromImage(b);
            gr.DrawImageUnscaled(pictureBox1.Image, new Rectangle(x, y, w+x, h+y));
            pictureBox2.Image = b;
            pictureBox2.Image.Save("C:/"+xF.ToString()+"x"+yF.ToString()+".jpg");
            pictureBox2.Image = null;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int sCoutWidth, sCoutHeigth;
            sCoutWidth = pictureBox1.ClientRectangle.Width / 175;
            sCoutHeigth = pictureBox1.ClientRectangle.Height / 175;
            label1.Text = sCoutWidth.ToString() + "X" + sCoutHeigth.ToString();
            for (int i = 0; i < sCoutWidth; i++)
            {
                for (int j = 0; j < sCoutHeigth; j++)
                {
                    CopyRest1(j * 175, i * 175, 175, 175, i, j);
                }
            }
        }
    }
}
